﻿#region Copyright

// The contents of this file are subject to the Mozilla Public License
//  Version 1.1 (the "License"); you may not use this file except in compliance
//  with the License. You may obtain a copy of the License at
//  
//  http://www.mozilla.org/MPL/
//  
//  Software distributed under the License is distributed on an "AS IS"
//  basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
//  License for the specific language governing rights and limitations under 
//  the License.
//  
//  The Initial Developer of the Original Code is Robert Smyth.
//  Portions created by Robert Smyth are Copyright (C) 2008.
//  
//  All Rights Reserved.

#endregion

using System;
using System.Collections.Generic;
using NUnitGrid.Framework.Logging;
using NUnitGrid.Framework.Pipes;
using NUnitGrid.Protocol;
using NUnitGrid.Protocol.Responses;


namespace NUnitGrid.Server.Plugins.Clients.Client.Protocol
{
    public class RunAsProcessCommandHandler : IProtocolCommandHandler
    {
        private readonly IClientProxy clientProxy;
        private readonly ILogger logger;
        private readonly IPipe pipe;

        public RunAsProcessCommandHandler(IPipe pipe, IClientProxy clientProxy, ILogger logger)
        {
            this.pipe = pipe;
            this.clientProxy = clientProxy;
            this.logger = logger;
        }

        public bool CanHandle(string commandMessage)
        {
            return commandMessage.StartsWith("Command.RunOnGrid.AsProcess(");
        }

        public void Handle(string commandMessage)
        {
            IProtocolMessageParsing parser = new ProtocolMessageParser(commandMessage);
            var transactionID = parser.GetTransactionID();
            var fileName = parser.GetValue("FileName");
            var workingDirectory = parser.GetValue("WorkingDirectory");
            var commandLine = parser.GetValue("CommandLine");
            var timeLimit = TimeSpan.Parse(parser.GetValue("TimeLimit"));
            var clientMachineName = parser.GetValue("ClientMachineName");
            var sessionID = new Guid(parser.GetValue("SessionID"));

            IProtocolMessageParsing nestedPropertiesParser =
                new ProtocolMessageParser(parser.GetValue("EnvironmentVariables"));
            var enviromentVariableNames = new List<string>(nestedPropertiesParser.GetPropertyNames());
            var enviromentVariableName = string.Empty;
            var enviromentVariableValue = string.Empty;
            if (enviromentVariableNames.Count == 1) // >>>
            {
                enviromentVariableName = enviromentVariableNames[0].Trim();
                enviromentVariableValue = nestedPropertiesParser.GetValue(enviromentVariableName);
            }

            var task = new RunAsProcessTask(clientMachineName, fileName, workingDirectory, commandLine,
                                            timeLimit, enviromentVariableName, enviromentVariableValue, sessionID);

            var responseMessage = new ProtocolAppResponseMessage("RunOnGrid.AsProcess", transactionID);
            var responseTaskOnCompletion = new TaskCompletedResponseSender(responseMessage, pipe);

            clientProxy.RunTask(task, responseTaskOnCompletion);
        }
    }
}